package logic

import (
	"context"
	"order/models/mysql"
	"order/orderrpc/internal/svc"
	"order/orderrpc/orderrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewOrderAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderAddLogic {
	return &OrderAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *OrderAddLogic) OrderAdd(in *orderrpc.OrderAddRequest) (*orderrpc.OrderAddResponse, error) {
	// todo: add your logic here and delete this line
	var order mysql.Order
	//if order.Number == 0 {
	//	return nil, fmt.Errorf("商品库存充足")
	//}

	//var o mysql.Order
	//err := o.FindOrderById(int(in.OrderId))
	//if err != nil {
	//	return nil, fmt.Errorf("商品不存在")
	//}

	in.Number += 100

	order = mysql.Order{
		Name:   in.Name,
		Price:  float64(in.Price),
		Number: int(in.Number),
		Status: in.Status,
	}
	mysql.DB.Debug().Create(&order)
	return &orderrpc.OrderAddResponse{
		OrderId: int64(order.ID),
	}, nil
}
